<!DOCTYPE html>
<html>
<#include "include/header.html"/>
  <body id="snatch">
    <div class="page">
      <div class="content">
        <img src="${ctx}/resource/images/banner.png" class="banner">
        <div class="wrap">
          <div class="band">
          <#if pocket ?? >
            <div class="coins" id="surplus_currency" >剩余：<span>${pocket.surplus_currency!}</span>活力币</div>
            <#else>
            <div class="coins" id="surplus_currency" >剩余：<span>0</span>活力币</div>
            </#if>
            <#if pocket ?? >
            <img src="${ctx}/resource/images/snatch/btn_others.png" onclick="getRobRecord('${pocket.id!}');" class="btn">
            <#else>
            <img src="${ctx}/resource/images/snatch/btn_others.png" onclick="getRobRecord('');" class="btn">
            </#if>
          </div>
          <div class="main">
            <#if pocket ?? >
                  <button  id="rob">抢</button>   <!-- 抢红包按钮 -->
            <#else>
                  <button  id="rob">抢</button>   <!-- 抢红包按钮 -->
            </#if>
          </div>
          <div class="record-wrapper">
          <div class="record" >
            <header>我的记录</header>
            <ul id="rob_record">
            <#if robPockeList ?? >
              <#list robPockeList as robPocke>
              <li><span>${robPocke.df_date!}</span><span>${robPocke.df_time!}</span><span>抢了${robPocke.get_currency!}个活力币</span></li>
              </#list>
            </ul>
          </div>
          <div id="morePage">
          <#if pageNumber lt totalPage>
                <img id="more_page" src="${ctx}/resource/images/snatch/more.png" onclick="nextPage(${pageNumber!}); return false;" class="more">
          </#if>
          </div>
         </#if>
        </div>
        
      </div>
      
      <!-- 查看失败 -->
      <div class="dialog fail">
        <div class="d-body">
          <p class="message">系统繁忙 请稍后再试</p>
        </div>
        <div class="d-footer">
          <img src="${ctx}/resource/images/index/dialog_btn_back.png" class="btn one back" alt="" onclick="dialogClose()"> <!--返回按钮-->
        </div>
      </div>
      <!-- 兑换成功 -->
      <div class="dialog success">
        <div class="d-body">
          <p>我抢到了 <span class="amount">9</span> 活力币，<br>快通知我的小伙伴吧！</p>
        </div>
        <div class="d-footer">
          <img src="${ctx}/resource/images/snatch/continue.png" class="btn one back" alt="" onclick="dialogClose()"> <!--继续抢按钮-->
        </div>
      </div>
    </div>
  </div>
  <div class="mask"></div>
  <script>
  var fromUser1=$.cookie('fromUser1');
  var pocket_id="";
  <#if pocket??>
    pocket_id="${pocket.id!}";
  </#if>
  //$("#rob").click(robPocket);
  var tabEnterTime = 0;
 $("#rob").on("touchstart mousedown", function(e) {
  e.stopPropagation()
  e.preventDefault()
  tabEnterTime = Date.now();
}).on("touchend mouseup", function() {
  if (Date.now() - tabEnterTime < 1000) {
    robPocket();
  }
})
  function robPocket(){
     $("#rob").off("touchstart touchend mousedown mouseup");
    statistics(); //cnzz统计
    $.ajax({
    type:"get",
    url : "${ctx}/home/robSnatch",
    data:{pocket_id:pocket_id},
    async : false,	
    success:function(json){
     if(json.code == 0 ){
      $(".success").find(".amount").text(json.data.get_curry);
       //显示对话框
      dialog(".success");
      $("#surplus_currency").html("剩余：<span>"+json.data.surplus_currency+"</span>活力币");
      $("#rob_record").prepend("<li><span>"+json.data.df_date+"</span><span>"+json.data.df_time+"</span><span>抢了"+json.data.get_curry+"个活力币</span></li>");
     }else{
     if(json.code == -3){
       window.location.href="https://api.weibo.com/oauth2/authorize?client_id=1438888984&response_type=code&scope=follow_app_official_microblog&redirect_uri=http://lbgj.fansiji.com/xyChampion/home/homePage?forwardType=1";
     }else{
         $(".dialog.fail").find(".message").text(json.msg);
         dialog(".fail");
         if(json.code == -4){
          $("#surplus_currency").html("剩余：<span>"+json.data.surplus_currency+"</span>活力币");
         }
         }
     }
     $("#rob").on("touchstart mousedown", function(e) {
        e.stopPropagation()
        e.preventDefault()
        tabEnterTime = Date.now();
     }).on("touchend mouseup", function() {
	  if (Date.now() - tabEnterTime < 1000) {
	    robPocket();
	  }
    })
    }
    });
  }
  function nextPage(page){
    statistics(); //cnzz统计
    $("#more_page").remove();
    $.post("${ctx}/home/robPocketPage",{
     page:page+1
    },function(json){
      var htm="";
      var pageHtm="";
      var list=json.list;
      for(var i in list){
      htm+="<li><span>"+list[i].df_date+"</span><span>"+list[i].df_time+"</span><span>抢了"+list[i].get_currency+"个活力币</span></li>";
      }
      if(json.pageNumber < json.totalPage){
				pageHtm+="<img id='more_page' src='${ctx}/resource/images/snatch/more.png' onclick='nextPage("+json.pageNumber+"); return false;' class='more'>";
			}
      $("#rob_record").append(htm);
      $("#morePage").html(pageHtm);
    });
  }
  function getRobRecord(pocket_id){
   statistics(); //cnzz统计
   if(pocket_id == ''){
         $(".dialog.fail").find(".message").text("暂无记录");
         dialog(".fail");
         return;
   }
   if(!fromUser1){
     window.location.href="https://api.weibo.com/oauth2/authorize?client_id=1438888984&response_type=code&scope=follow_app_official_microblog&redirect_uri=http://lbgj.fansiji.com/xyChampion/home/homePage?forwardType=1";
   }else{
     window.location.href="${ctx}/home/PocketRecord?pocket_id="+pocket_id;     
   }
  }
  </script>
</body>
</html>